<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="renderer" content="webkit">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title></title>
    @include('common-css')
    <style>

        .input_box {
            width: 100%;
            height: 40px;
            margin-top: 15px;
            margin-bottom: 15px;
            display: flex;
            justify-content: flex-start;
            align-items: center;
        }

        .input_label {
            width: 120px;
            box-sizing: border-box;
        }

        .project-table {
            margin-top: 15px;
        }

        .status-tag {
            color: #1890ff;
            border: 1px solid #91d5ff;
            border-radius: 3px;
            background: #e6f7ff;
            font-size: 12px;
            padding: 2px 3px;
        }

        .ant-table-tbody > tr > td, .ant-table-thead > tr > th {
            vertical-align: middle;
            line-height: 25px;
        }

        .search_box {
            width: 1000px;
        }

        .search_box input {
            width: 300px;
        }

        .search_box button {
            margin-left: 20px;
        }

        .search_box .ant-select {
            margin-left: 20px;
        }
    </style>
</head>
<body>

<div id="app"></div>

<script id="appTemplate" type="x-template">
    <a-locale-provider :locale="antdLocale">
        <div class="container">

            @include('committee.header-tpl')

            <main>
                <div class="common_container">

                    @include('committee.aside-tpl')

                    <div class="common_content admin_common_content">
                        <div style="margin-top: 20px; margin-bottom: 20px">
                            <template v-if="atPhaseDefense">
                                <a-popconfirm title="提交后不能撤销，继续？" okText="是" cancelText="否"
                                              @confirm="handleSubmitDefenseResult">
                                    <a-button type="primary">提交答辩结果</a-button>
                                </a-popconfirm>
                            </template>
                            <a-button v-else disabled type="primary">提交答辩结果</a-button>
                        </div>

                        <a-table class="project-table"
                                 rowKey="project_id"
                                 size="small"
                                 :columns="projectTableColumns"
                                 :dataSource="filteredProjects"
                                 :loading="loadingPageData"
                                 :pagination="{pageSize: 10000, hideOnSinglePage: true, position: 'both', showTotal: total => `共 ${total} 项`}">
                            <span slot="application_form" size="large" slot-scope="text, record">
                                <a :href="'/home/view-form?id=' + record.project_id" target="_blank">查看</a>
                            </span>
                            <span slot="attachments" slot-scope="text, record">
                                <a :href="'/home/view-attachments?project_id=' + record.project_id" target="_blank">
                                    <span>查看</span>
                                </a>
                            </span>
                            <span slot="score_time_costs" size="large" slot-scope="text, record">
                                <template v-for="reviewer_score in record.score_per_reviewer">
                                    <span v-html="formatScoreTimeCost(reviewer_score.score_time_cost)"/><br>
                                </template>
                            </span>
                            <span slot="reviewer_scores" size="large" slot-scope="text, record">
                                <template v-for="reviewer_score in record.score_per_reviewer">
                                    <span>@{{ reviewer_score.score !== null ? reviewer_score.score : '-' }}</span><br>
                                </template>
                                <label v-if="!record.scores_normal"
                                       style="color: red;">@{{ record.scores_desc }}</label>
                            </span>
                            <span slot="is_evidence_material_valid" size="large" slot-scope="text, record">
                                <template v-for="reviewer_score in record.score_per_reviewer">
                                    <label>@{{ reviewer_score.is_evidence_material_valid === null ? '-' :
                                        (reviewer_score.is_evidence_material_valid === 1 ? '是' : '否') }}</label>
                                    <br>
                                </template>
                            </span>
                            <span slot="reviewer_remarks" size="large" slot-scope="text, record">
                                <template v-for="reviewer_score in record.score_per_reviewer">
                                    <a-textarea :disabled="true" :autosize="{minRows: 1, maxRows: 10}"
                                                v-model="reviewer_score.remark"/>
                                    <br>
                                </template>
                            </span>
                            <span slot="avg_score" size="large" slot-scope="text, record">
                                @{{ record.avg_score !== null ? record.avg_score : '-' }}
                            </span>
                            <span slot="final_award_level" slot-scope="text, record">
                                <a-select :disabled="!atPhaseDefense" style="width: 140px;" size="small"
                                          v-model="record.final_award_level"
                                          @change="handleUpdateDefenseAwardLevel(record)">
                                    <a-select-option  value="特等奖">特等奖</a-select-option>
                                    <a-select-option  value="一等奖">一等奖</a-select-option>
                                </a-select>
                            </span>
                            </span>
                        </a-table>
                    </div>

                </div>
            </main>

        </div>
    </a-locale-provider>
</script>

@include('common-js')
@include('committee.header-js')
@include('committee.aside-js')

<script>
    var queryParams = parseUrl(location.href).params;
    var awardsInfo = {!! json_encode(array_values(config('vars.awards')), JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) !!};

    var projectTableColumns = [
        {
            width: 60,
            title: '序号',
            dataIndex: 'num'
        },
        {
            width: 150,
            title: '项目名称',
            dataIndex: 'project_name',
        },
        {
            width: 100,
            title: '申报者',
            dataIndex: 'applicant',
        },
        {
            width: 60,
            title: '申报书',
            dataIndex: 'application_form',
            scopedSlots: {
                customRender: 'application_form'
            }
        },
        {
            width: 60,
            title: '附件',
            dataIndex: 'attachments',
            scopedSlots: {
                customRender: 'attachments'
            }
        },
        {
            width: 100,
            title: '评分时长',
            dataIndex: 'score_time_costs',
            scopedSlots: {
                customRender: 'score_time_costs'
            }
        },
        {
            width: 70,
            title: '专家评分',
            dataIndex: 'reviewer_scores',
            scopedSlots: {
                customRender: 'reviewer_scores'
            }
        },
        {
            width: 70,
            title: '附件证明材料是否真实有效',
            dataIndex: 'is_evidence_material_valid',
            scopedSlots: {
                customRender: 'is_evidence_material_valid'
            }
        },
        {
            width: 200,
            title: '专家意见',
            dataIndex: 'reviewer_remarks',
            scopedSlots: {
                customRender: 'reviewer_remarks'
            }
        },
        {
            width: 60,
            title: '平均分',
            dataIndex: 'avg_score',
            scopedSlots: {
                customRender: 'avg_score'
            }
        },
        {
            width: 80,
            title: '授奖等级',
            key: 'final_award_level',
            scopedSlots: {
                customRender: 'final_award_level'
            }
        },
    ];
</script>

<script>
    var app = new Vue({
        el: '#app',
        template: '#appTemplate',
        data: {
            profCategoryFilter: '',
            searchKeyword: '',

            projectTableColumns,
            pageData: [],
            loadingPageData: false,

            largeGroupId: -1,

            queryParams,
            awardsInfo,
            profCategories,

            asideSelectedKeys:
                queryParams.aside_key ? [queryParams.aside_key] : [],
            antdLocale:
            antd.locales.zh_CN,
            ...CommitteeHeader.data,
            ...CommitteeAside.data,
            ...Workflow.data,
        },
        computed: {
            atPhaseDefense() {
                return this.atPhases(['I'], this.largeGroupId)
            },
            filteredProjects() {
                var projects = this.searchFilteredProjects;
                if (this.profCategoryFilter) {
                    projects = projects.filter(project => {
                        return project.prof_category == this.profCategoryFilter
                    })
                }
                var num = 1;
                projects.forEach(project => {
                    project.num = num++
                });
                return projects
            },
            searchFilteredProjects() {
                var projects;
                if (this.searchKeyword) {
                    var q = this.searchKeyword.trim();
                    projects = this.pageData.filter(project => {
                        var text = project.project_name + project.applicant_name + project.applicant_org_name + project.applicant_tel + project.applicant_email;
                        return text.toLowerCase().indexOf(q) != -1
                    })
                } else {
                    projects = this.pageData
                }
                return projects
            },
        },
        mounted() {
            this.reloadPageData();
            this.reloadWorkflowPhases()
        },
        methods: {
            handleSubmitDefenseResult() {
                axios.post('/api/committee/leader-submit-defense-result').then(response => {
                    if (response.data.ok) {
                        success('提交成功');
                        this.reloadWorkflowPhases()
                    } else {
                        error('提交失败: ' + response.data.msg)
                    }
                })
            },
            handleUpdateDefenseAwardLevel(project) {
                axios.post('/api/committee/update-defense-award-level', {
                    project_id: project.project_id,
                    defense_award_level: project.final_award_level,
                }).then(response => {
                    let data = response.data;
                    if (data.ok) {
                        success('修改授奖等级成功')
                    } else {
                        error('修改授奖等级失败: ' + data.msg)
                    }
                })
            },
            formatScoreTimeCost(scoreTimeCost) {
                if (scoreTimeCost === null) {
                    return '-'
                } else if (scoreTimeCost < 60) {
                    return `<span style="color: red;">${scoreTimeCost}秒</span>`
                } else if (scoreTimeCost < 3600) {
                    let minutes = parseInt(scoreTimeCost / 60);
                    let seconds = scoreTimeCost % 60;
                    let content = `${minutes}分钟${seconds}秒`;
                    return scoreTimeCost < 300 ? `<span style="color: red;">${content}</span>` : content
                } else if (scoreTimeCost < 86400) {
                    let hours = parseInt(scoreTimeCost / 3600);
                    let minitues = Math.round(scoreTimeCost % 3600 / 60);
                    return `${hours}小时${minitues}分钟`
                } else {
                    let days = parseInt(scoreTimeCost / 86400);
                    let hours = Math.round(scoreTimeCost % 86400 / 3600);
                    return `${days}天${hours}小时`
                }
            },
            reloadPageData() {
                this.loadingPageData = true;
                axios.get('/api/committee/get-manage-large-group-defense-data').then(response => {
                    let data = response.data;
                    if (data.ok) {
                        this.pageData = data.projects;
                        this.largeGroupId = data.large_group_id;
                    } else {
                        error('加载页面数据失败: ' + data.msg)
                    }
                    this.loadingPageData = false
                })
            },
            ...CommitteeHeader.methods,
            ...CommitteeAside.methods,
            ...Workflow.methods,
        }
    })
</script>

</body>
</html>